<!DOCTYPE html>
<html>
<head>
    <title>PlayCanvas Basis Textures</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
    <link rel="icon" type="image/png" href="../playcanvas-favicon.png" />
    <script src="../../build/output/playcanvas.js"></script>
    <style>
        body { 
            margin: 0;
            overflow-y: hidden;
        }
    </style>
</head>

<body>
    <!-- The canvas element -->
    <canvas id="application-canvas"></canvas>

    <!-- The script -->
    <script>

        pc.basisDownload(
            '../lib/basis/basis.wasm.js',
            '../lib/basis/basis.wasm.wasm',
            '../lib/basis/basis.js',
            function () {

                var canvas = document.getElementById("application-canvas");

                // Create the application and start the update loop
                var app = new pc.Application(canvas);

                app.start();

                // Set the canvas to fill the window and automatically change resolution to be the same as the canvas size
                app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW);
                app.setCanvasResolution(pc.RESOLUTION_AUTO);

                window.addEventListener("resize", function () {
                    app.resizeCanvas(canvas.width, canvas.height);
                });

                app.scene.ambientLight = new pc.Color(1, 1, 1);

                // Create a Entity with a Box model component
                var box1 = new pc.Entity();
                box1.addComponent("model", { type: "box", });

                var box2 = new pc.Entity();
                box2.addComponent("model", { type: "box", });

                // Create an Entity with a camera component
                var camera = new pc.Entity();
                camera.addComponent("camera", {
                    clearColor: new pc.Color(0.4, 0.45, 0.5)
                });

                // Add the new Entities to the hierarchy
                app.root.addChild(box1);
                app.root.addChild(box2);
                app.root.addChild(camera);

                box1.setPosition(-1, 0, 0);
                box2.setPosition( 1, 0, 0);

                // Move the camera 10m along the z-axis
                camera.translate(0, 0, 4);

                // Set an update function on the app's update event
                var angle = 0;
                app.on("update", function (dt) {
                    angle += dt;
                    if (angle > 360) {
                        angle = 0;
                    }

                    // Move the light in a circle
                    // light.setLocalPosition(3 * Math.sin(angle), 0, 3);

                    // Rotate the box
                    box1.setEulerAngles(angle*2, angle*4, angle*8);
                    box2.setEulerAngles(90-angle*12, 120-angle*8, 150-angle*10);
                });

                // load a texture, create material and apply to box
                app.assets.loadFromUrl(
                    "../assets/textures/sea.basis",
                    "texture",
                    function (err, asset) {
                        var material = new pc.StandardMaterial();
                        material.diffuseMap = asset.resource;
                        material.update();
                        box1.model.meshInstances[0].material = material;
                    }
                );

                app.assets.loadFromUrl(
                    "../assets/textures/playcanvas.basis",
                    "texture",
                    function (err, asset) {
                        var material = new pc.StandardMaterial();
                        material.diffuseMap = asset.resource;
                        material.update();
                        box2.model.meshInstances[0].material = material;
                    }
                );
            });
    </script>
</body>
</html>
